<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!-- import Axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <style>
        body {
            margin: 0; /*去掉自带外边距*/
            background-image: url("img/probackground.jpg");
            /*cover设置封面效果*/
            background-size: cover;
            text-align: center;
        }

        h1 {
            font-size: 72px;
            color: #009688;
            margin-bottom: 0;
        }

        img {
            width: 100px
        }

        h2 {
            font-size: 32px;
            color: #009688;
            margin-bottom: 0;
        }

    </style>
</head>
<body>
<div id="app">
    <h1>欢迎来到果真好商城</h1>
    <img src="logo.jpg" alt="">
    <h2>FruitShopping</h2>
    <el-card style="width: 880px;height: 750px;margin: 0 auto;
background-color: rgba(255,255,255,0.6)">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username" @blur="getUsername()"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
                <template>
                    <el-radio v-model="ruleForm.sex" label="1">男</el-radio>
                    <el-radio v-model="ruleForm.sex" label="2">女</el-radio>
                </template>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label="昵称" prop="nickname">
                <el-input v-model="ruleForm.nickname"></el-input>
            </el-form-item>
            <el-form-item label="联系方式" prop="phone">
                <el-input v-model="ruleForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
                <el-input v-model="ruleForm.email"></el-input>
            </el-form-item>
            <!--<el-form-item label="是否启用" prop="enable">
                <el-switch
                        v-model="ruleForm.enable"
                        :active-value="1"
                        :inactive-value="0"
                        active-color="#13ce66"
                        inactive-color="#ff4949">
                </el-switch>
            </el-form-item>-->
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>

</div>
</body>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
    let v = new Vue({
        el: 'body>div',
        data: function () {
            return {
                ruleForm: {
                    username: '',
                    sex: '1',
                    password: '',
                    nickname: '',
                    phone: '',
                    email: '',
                    address: '',
                    enable: 1,
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
                    ],
                    nickname: [
                        {required: true, message: '请输入昵称', trigger: 'blur'},
                        {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '请输入联系方式', trigger: 'blur'},
                        {min: 8, max: 15, message: '长度在 8 到 15 个字符', trigger: 'blur'}
                    ],
                    email: [
                        {required: true, message: '请输入邮箱', trigger: 'blur'},
                        {min: 6, max: 30, message: '长度在 6 到 30 个字符', trigger: 'blur'}
                    ],
                    address: [
                        {required: true, message: '请输入地址', trigger: 'blur'},
                        {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur'}
                    ],
                    roleIds: [
                        {required: true, message: '角色id至少添加一个', trigger: 'blur'},
                    ],

                }
            };
        },
        methods: {
            getUsername() {
                axios.get("/user/getUsername?username=" + this.ruleForm.username).then((response) => {
                    let responseBody = response.data;
                    if (responseBody.state != 20000) {
                        this.$message.error(responseBody.message);
                    }
                });
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if(this.ruleForm.sex == 1){
                            this.ruleForm.sex = '男';
                        }else {this.ruleForm.sex = '女';}
                        axios.post("user/reg",this.ruleForm).then((response) => {
                            let responseBody = response.data;
                            if (20000 == responseBody.state) {
                                this.$message({message: "操作成功", type: "success"})
                                this.resetForm(formName);
                                location.href='login.html';

                            } else {
                                this.$message.error(responseBody.message)
                            }
                            console.log(responseBody)
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        mounted() {

        }
    })
</script>
</html>
